## RMatrix to numpyArray.  Converts a structuredDict signal to an RDataFrame signal.  Should be a strait forward connect and send.

"""
<name>RMatrix to Array</name>
<description>Converts an R matrix into a numpy array signal.  This is useful when using functions that expect numpy arrays.  A companion function Array to RMatrix does the opposite.</description>
<tags>Data Conversion</tags>
<RFunctions></RFunctions>
<icon></icon>
"""


from OWRpy import *
# import OWGUI
import signals
import redRGUI 


class RMatrixToArray(OWRpy):
    def __init__(self, parent=None, signalManager=None):
        OWRpy.__init__(self, parent, signalManager, 'RMatrixToArray')
        
        self.setRvariableNames(["conversionDataFrame"])
        
        self.inputs = [('Data', signals.RMatrix, self.gotData)]
        self.outputs = [('Data', signals.conversion.numpyArray)]
        
        ### GUI ###
        self.infoArea = redRGUI.textEdit(self.controlArea)
        self.infoArea.setHtml('Data not yet processed')
    def gotData(self, data):
        if data:
            ### start the conversion ###
            
            newArray = self.R('as.matrix('+data.getData()+')', wantType = 'array')
            print newArray, 'newArray'
            newData = signals.conversion.numpyArray(data = newArray)
            self.rSend('Data', newData)
            self.infoArea.clear()
            
            self.infoArea.setHtml('Your data has been processed. It looks like this now:</br></br><pre>'+str(newArray)+'</pre>')
        else:
            self.infoArea.setHtml('No data to process')
